package com.mzj.saas.mservice.eligible.controller;

import com.mzj.saas.commons.ApiData;
import com.mzj.saas.commons.CustomException;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.eligible.service.ApproveService;
import com.mzj.saas.mservice.eligible.vo.*;
import com.mzj.saas.mservice.house.vo.HouseInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 资格审批接口
 */
@Controller
@RequestMapping("/approve")
public class ApproveController {

    @Autowired
    private ApproveService approveService;

    /**
     * 资格审核列表
     *
     * @param accessToken
     * @param status
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/eligibleList")
    @ResponseBody
    public ResultBean<EligibleApiData<EligibleListVO>> eligibleList(@RequestHeader String accessToken,
                                                                    @RequestParam(value = "status") Integer status,
                                                                    @RequestParam(defaultValue = "0") Integer pageNumber,
                                                                    @RequestParam(defaultValue = "5") Integer pageSize) {
        return approveService.eligibleList(accessToken, status, pageNumber, pageSize);
    }

    /**
     * 企业端-资格审核列表
     *
     * @param accessToken 权限鉴定
     * @param name  申请人
     * @param type  申请类型
     * @param applyArea 申请区域
     * @param pageNumber 当前页
     * @param pageSize 	页数
     * @return
     */
    @GetMapping("/eligibleList2")
    @ResponseBody
    public ResultBean<ApiData<EligibleListVO>> eligibleList2(@RequestHeader String accessToken,
                                                             @RequestParam(value = "name", required = false) String name,
                                                             @RequestParam(value = "type", required = false) Integer type,
                                                             @RequestParam(value = "applyArea", required = false) String applyArea,
                                                             @RequestParam(value = "applyStatus", required = false) Integer applyStatus,
                                                             @RequestParam(value = "matchStatus", required = false) Integer matchStatus,
                                                             @RequestParam(defaultValue = "0") Integer pageNumber,
                                                             @RequestParam(defaultValue = "5") Integer pageSize) {
        return approveService.eligibleList2(accessToken, name, type, applyArea, applyStatus, matchStatus, pageNumber, pageSize);
    }

    /**
     * 审批按钮状态,实现了判断数据是否是0：按钮可审批，1：按钮不可审批，2：按钮已审批
     *
     * @param accessToken 权限鉴定
     * @param applyId 申请ID
     * @return
     */
    @GetMapping("/approveStatus")
    @ResponseBody
    public ResultBean<Integer> approveStatus(@RequestHeader String accessToken,
                                             @RequestParam(value = "applyId") Long applyId,
                                             @RequestParam(value = "type", required = false) Integer type) {
        return approveService.approveStatus(accessToken, applyId, type);
    }

    /**
     * 审批操作,实现了手动进行公租房的审批流程
     *
     * @param applyId 申请ID
     * @param auditStatus 0未审批 1待审批 2审批通过 3审批不通过
     * @param remark 审批备注
     * @return
     */
    @GetMapping("/approveEligible")
    @ResponseBody
    public ResultBean<Object> approveEligible(@RequestHeader String accessToken,
                                              @RequestParam(value = "applyId") Long applyId,
                                              @RequestParam(value = "auditStatus") Integer auditStatus,
                                              @RequestParam(value = "remark") String remark) {
        return approveService.approveEligible(accessToken, applyId, auditStatus, remark);
    }

    /**
     * 轮候配租按钮状态，实现了判断数据0:可轮候配租，1:不可轮候配租
     *
     * @param accessToken 权限鉴定
     * @return
     */
    @GetMapping("/waitingRentStatus")
    @ResponseBody
    public ResultBean<Integer> waitingRentStatus(@RequestHeader String accessToken) {
        return approveService.waitingRentStatus(accessToken);
    }

    /**
     * 轮候配租列表
     *
     * @param accessToken 权限鉴定
     * @param applyId 申请ID
     * @param backlogItemId 待办事项ID
     * @return
     */
    @GetMapping("/waitingRent")
    @ResponseBody
    public ResultBean<WaitingRentDataVO<WaitingRentVO>> waitingRent(@RequestHeader String accessToken,
                                                                    @RequestParam(value = "applyId", required = false) Long applyId,
                                                                    @RequestParam(value = "backlogItemId", required = false) Long backlogItemId) {
        return approveService.waitingRent(accessToken, applyId, backlogItemId);
    }

    /**
     * 轮候配租列表-待办事项已完成
     *
     * @param accessToken
     * @param backlogItemId
     * @return
     */
    @GetMapping("/backlogWaitingRent")
    @ResponseBody
    public ResultBean<WaitingRentVO> backlogWaitingRent(@RequestHeader String accessToken,
                                                        @RequestParam(value = "backlogItemId") Long backlogItemId) {
        return approveService.backlogWaitingRent(accessToken, backlogItemId);
    }

    /**
     * 查看空置房
     *
     * @param orgId
     * @param districtId
     * @return
     */
    @GetMapping("/emptyHouse")
    @ResponseBody
    public ResultBean<List<Map<String, Object>>> emptyHouse(@RequestParam(value = "orgId") Long orgId,
                                                            @RequestParam(value = "districtId") Long districtId,
                                                            @RequestParam(value = "applyId", required = false) Long applyId) {
        return approveService.emptyHouse(orgId, districtId, applyId);
    }

    /**
     * 轮候配租-匹配操作
     *
     * @param accessToken
     * @param applyId
     * @param matchUserId
     * @param orgId
     * @param residenceCommunityId
     * @return
     * @throws CustomException 
     */
    @GetMapping("/rent")
    @ResponseBody
    public ResultBean<Object> rent(@RequestHeader String accessToken,
                                   @RequestParam(value = "applyId") Long applyId,
                                   @RequestParam(value = "matchUserId") String matchUserId,
                                   @RequestParam(value = "orgId") Long orgId,
                                   @RequestParam(value = "residenceCommunityId") String residenceCommunityId) throws CustomException {
        return approveService.rent(accessToken, applyId, matchUserId, orgId, residenceCommunityId);
    }

    /**
     * 查看房屋信息
     *
     * @param accessToken
     * @param applyId
     * @return
     */
    @GetMapping("/findHouse")
    @ResponseBody
    public ResultBean<HouseInfoVO> findHouse(@RequestHeader String accessToken,
                                             @RequestParam(value = "applyId") Long applyId) {
        return approveService.findHouse(accessToken, applyId);
    }

    /**
     * 批量审批
     *
     * @param accessToken
     * @param vo
     * @return
     */
    @PostMapping("/batchApprove")
    @ResponseBody
    public ResultBean<Object> batchApprove(@RequestHeader String accessToken,
                                           @RequestBody BatchApproveVO vo) {
        return approveService.batchApprove(accessToken, vo);
    }


}
